package org.gdufe.books.controller;

import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;
import org.gdufe.books.service.BookService;
import org.gdufe.pojos.dto.InsertBookDto;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.pojo.Book;
import org.gdufe.pojos.vo.R;
import org.gdufe.pojos.vo.PageResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.sql.Driver;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    //分页查找
    @GetMapping("/querry")
    public PageResponseResult querryBook( PageParams pageParams, @RequestParam(required = false) String title){
        return bookService.querryBooks(title,pageParams);
    }


    //新增书籍
    @PostMapping("/insert")
    @PreAuthorize("hasAuthority('super')")
    public R insertBook(@RequestBody InsertBookDto insertBookDto){
        return bookService.insertBook(insertBookDto);
    }

    //借出
    @GetMapping("/borrow/{id}")
    public R borrow(@PathVariable Integer id){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return bookService.borrow(id,username);
    }

    //归还
    @GetMapping("/return/{id}")//id : 书籍id
    public R returnBook(@PathVariable Integer id){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return bookService.returnBook(id,username);
    }

    //查看排行榜
    @GetMapping("/top")
    public R querryTops(){
        return bookService.querryTops();
    }

    //删除书籍
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('super')")
    public R deleteBook(Integer id){
        return bookService.deleteBook(id);
    }

    //更新书籍
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('super')")
    public R updateBook(@RequestBody Book book){
        return bookService.updateBook(book);
    }

    //前端不用写
    @GetMapping("/listId")
    @PreAuthorize("hasAuthority('super')")
    public R listId(){
        return bookService.listBId();
    }

    //前端不用写
    @GetMapping("/updatePoint")
    @PreAuthorize("hasAuthority('super')")
    public R updatePoint(@RequestParam("id") Integer id,@RequestParam("point") Double point){
        return bookService.updatePoint(id,point);
    }


}
